package com.yys.huarongdao;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.yys.huarongdao.application.MyApplication;
import com.yys.huarongdao.dao.MySQLiteOpenHelper;
import com.yys.huarongdao.utils.BgmUtil;

public class HomeActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_account;
    private TextView tv_ranking_list3;
    private TextView tv_ranking_list4;
    private TextView tv_ranking_list5;

    private Button but_start_game;
    private Button but_set_rank;
    private Button but_set_bgm;
    private Button but_ranking_list;
    private Button but_exit;

    private String account;

    private final static int SET_RANK=0;
    private int rankType=3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }
        //设置全屏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_home);
    }

    @Override
    protected void onResume() {
        super.onResume();
        bindViews();
    }

    private void bindViews(){
        tv_account = findViewById(R.id.tv_account);
        tv_ranking_list3=findViewById(R.id.tv_ranking_list3);
        tv_ranking_list4=findViewById(R.id.tv_ranking_list4);
        tv_ranking_list5=findViewById(R.id.tv_ranking_list5);

        but_start_game = findViewById(R.id.but_start_game);
        but_set_rank = findViewById(R.id.but_set_rank);
        but_set_bgm = findViewById(R.id.but_set_bgm);
        but_ranking_list = findViewById(R.id.but_rank_list);
        but_exit = findViewById(R.id.but_exit);

        but_exit.setOnClickListener(this);
        but_ranking_list.setOnClickListener(this);
        but_set_bgm.setOnClickListener(this);
        but_set_rank.setOnClickListener(this);
        but_start_game.setOnClickListener(this);
        setViews();
    }

    private void setViews(){
//        account=getSharedPreferences("globalData",MODE_PRIVATE).getString("account",null);
        account= MyApplication.getApplication().getAccount();
        tv_account.setText("用户："+account);


        //获取3x3,4x4,5x5矩阵的最好成绩
        int rank3=getBestRanking(3);
        int rank4=getBestRanking(4);
        int rank5=getBestRanking(5);

        if(rank3!=-1)
            tv_ranking_list3.setText("3x3:第"+rank3+"名");
        else
            tv_ranking_list3.setText("3x3:未上榜");
        if(rank4!=-1)
            tv_ranking_list4.setText("4x4:第"+rank4+"名");
        else
            tv_ranking_list4.setText("4x4:未上榜");
        if(rank5!=-1)
            tv_ranking_list5.setText("5x5:第"+rank5+"名");
        else
            tv_ranking_list5.setText("5x5:未上榜");
    }

    //获取最好的排名
    private int getBestRanking(int rankType){
        int ranking=-1;
        MySQLiteOpenHelper sqLiteOpenHelper=new MySQLiteOpenHelper(this,"huarongdao.db",null,1);
        SQLiteDatabase database= sqLiteOpenHelper.getReadableDatabase();
        Log.d("account","getBestRanking,account:"+account+",rankType"+rankType);
        if(account==null)
            return -1;
        Cursor userCursor=database.rawQuery("select userid from user where account=?",new String[]{account});
        Cursor rankCursor=database.rawQuery("select *from ranking_list where rank_type=? order by time_spent asc,date asc",new String[]{String.valueOf(rankType)});
        if(!userCursor.moveToFirst()||!rankCursor.moveToFirst())
            return ranking;
        int userId=userCursor.getInt(0);
        int count=0;
        do{
            count++;
            if(userId==rankCursor.getInt(rankCursor.getColumnIndex("userid"))){
                return count;
            }
        }while(rankCursor.moveToNext());
        return ranking;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.but_start_game:
                Intent intent = new Intent(this,GameActivity.class);
                intent.putExtra("rankType",rankType);
                startActivity(intent);
                break;
            case R.id.but_set_rank:
                startActivity(new Intent(this,RankActivity.class));
                break;
            case R.id.but_set_bgm:
                startActivity(new Intent(this,BgmActivity.class));
                break;
            case R.id.but_rank_list:
                startActivity(new Intent(this,RankingListActivity.class));
                break;
            case R.id.but_exit:
                if(BgmUtil.BgmPlaying())
                    BgmUtil.closeBgm(this);
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
